// pages/addMoneyInput/addMoneyInput.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    unit: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getMoneyType(wx.getStorageSync('openid'))

  },

  // 获取当前用户的存钱类别
  getMoneyType(openid) {
    wx.cloud.callFunction({
      name: 'nine_thirth',
      config: {
        env: app.globalData.env
      },
      data: {
        cloud_function: "getMoneyType",
        openid: openid,
      }
    }).then((resp) => {
      console.log("信息：", resp)
      let data = resp.result.data
      if (data.status == 200) {
        let isNull = data.data.isNull
        if (isNull) {
          this.setData({
            showTip: true
          })
          this.getMoneyType('origin')
        } else {
          let addList = data.data.result[0].add
          let subList = data.data.result[0].sub
          this.setData({
            addUnit: addList,
            subUnit: subList
          })
        }
        wx.showToast({
          title: data.message,
          icon: 'none'
        })
      }
    }).catch((e) => {
      console.error('获取数据失败:', e);
    });
  },

  // 输入内容
  onUnitInput(e) {
    console.log(e);
    let index = e.target.dataset.index
    let value = e.detail.value
    let type = e.target.dataset.type
    let addUnit = this.data.addUnit
    let subUnit = this.data.subUnit
    if (type == 'addUnit') {
      addUnit[index].value = value
    } else if (type == 'subUnit') {
      subUnit[index].value = value
    }
    this.setData({
      addUnit: addUnit,
      subUnit: subUnit
    })
  },

  // 重置按钮
  origin() {
    let that = this
    wx.showModal({
      title: '确认重置?',
      content: '重置为系统初始数据哦',
      complete: (res) => {
        if (res.cancel) {
          wx.showToast({
            title: '嗯嗯',
            icon: 'none'
          })
        }

        if (res.confirm) {
          that.getMoneyType(wx.getStorageSync('openid'))

        }
      }
    })
  },

  // 提交按钮
  submit() {
    this.addMoneyType(this.data.addUnit, this.data.subUnit)
  },


  // 添加当前用户的存钱类别
  addMoneyType(addList, subList) {
    wx.cloud.callFunction({
      name: 'nine_thirth',
      config: {
        env: app.globalData.env
      },
      data: {
        cloud_function: "addMoneyType",
        openid: wx.getStorageSync('openid'),
        add: addList,
        sub: subList,
      }
    }).then((resp) => {
      console.log("信息：", resp)
      let data = resp.result.data
      if (data.status == 200) {
        wx.showToast({
          title: data.message,
          icon: 'none'
        })
      }
    }).catch((e) => {
      console.error('获取数据失败:', e);
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})